<template>
  <div class="search-box">
    <!-- 位置信息 -->
    <div class="location bottom-gray-line">
      <div class="city" @click="chooseCity">{{ currentCity.cityName }}</div>
      <div class="position" @click="gitMyPosition">
        <div class="textInfo">我的位置</div>
        <img src="@/assets/img/home/icon_location.png" alt="" />
      </div>
    </div>
    <!-- 日期范围 -->
    <div class="date-range section bottom-gray-line" @click="openCalendar">
      <div class="start">
        <div class="date">
          <div class="tip">入住</div>
          <div class="time">{{ startDate }}</div>
        </div>
        <div class="stay">共{{ stayDay }}晚</div>
      </div>
      <div class="end">
        <div class="date">
          <div class="tip">离店</div>
          <div class="time">{{ endDate }}</div>
        </div>
      </div>
    </div>
    <!-- 价格/人数选择 -->
    <div class="section price-counter bottom-gray-line">
      <div class="start">价格不限</div>
      <div class="end">人数不限</div>
    </div>
    <!-- 关键字 -->
    <div class="section keyword bottom-gray-line">关键字/位置/民宿名</div>

    <!-- 热门建议 -->
    <div class="section hot-suggests">
      <template v-for="(item, index) in hotSuggests" :key="index">
        <div
          class="item"
          :style="{
            color: item.tagText.color,
            background: item.tagText.background.color,
          }"
        >
          {{ item.tagText.text }}
        </div>
      </template>
    </div>
    <!-- 搜索按钮 -->
    <div class="section search-btn">
      <div class="btn" @click="searchBtnClick">开始搜索</div>
    </div>
    <van-calendar
      v-model:show="calendarShow"
      type="range"
      @confirm="onConfirm"
      color="#ff9645"
    />
  </div>
</template>

<script setup>
import { useRouter } from "vue-router";
import useCityStore from "@/stores/modules/city";
import useHomeStore from "@/stores/modules/home";
import { storeToRefs } from "pinia";
import { formatMonthDay, getDiffDays } from "@/utils/format_date";
import { ref } from "vue";
const router = useRouter();
/* 获取我的位置 */
const gitMyPosition = () => {
  navigator.geolocation.getCurrentPosition(
    (res) => {
      /* google浏览器不能正常获取 */
      console.log("获取位置成功");
      /* 获取到经纬度之后，去高德的sdk查询 */
    },
    (err) => {
      console.log("获取位置失败");
    },
    {
      enableHighAccuracy: true,
      timeout: 5000,
      maximumAge: 0,
    }
  );
};
/* 选择城市 */
const chooseCity = () => {
  router.push("/city");
};
const cityStore = useCityStore();
const { currentCity } = storeToRefs(cityStore);
/* 日期选择 */
const nowDate = new Date();
const newDate = new Date();
newDate.setDate(nowDate.getDate() + 1);
const startDate = ref(formatMonthDay(nowDate));
const endDate = ref(formatMonthDay(newDate));
const stayDay = ref(getDiffDays(nowDate, newDate));
/* 日期选择显示与否 */
const calendarShow = ref(false);
const onConfirm = (payload) => {
  console.log(payload[0]);
  startDate.value = formatMonthDay(payload[0]);
  endDate.value = formatMonthDay(payload[1]);
  stayDay.value = getDiffDays(payload[0], payload[1]);
  calendarShow.value = false;
};
const openCalendar = (payload) => {
  calendarShow.value = true;
};
/* 热门建议 */
const homeStore = useHomeStore();
homeStore.getHomeSuggestsAction();
const { hotSuggests, categories } = storeToRefs(homeStore);
/* 跳转搜索 */
const searchBtnClick = (payload) => {
  router.push({
    path: "/search",
    query: {
      startDate: startDate.value,
      endDate: endDate.value,
      currentCity: currentCity.value,
    },
  });
};
/* 热门推荐类别 */
homeStore.getHomeCategoriesAction();
</script>

<style lang="less" scoped>
.search-box {
  .location {
    height: 44px;
    display: flex;
    align-items: center;
    padding: 0px 20px;
    .city {
      flex: 1;
      color: #333;
      font-size: 15px;
    }
    .position {
      width: 74px;
      display: flex;
      align-items: center;

      .textInfo {
        font-size: 12px;
        position: relative;
        top: 1px;
        color: #333;
      }
      img {
        width: 18px;
        height: 18px;
        margin-left: 5px;
      }
    }
  }
  .section {
    display: flex;
    flex-wrap: wrap;
    align-items: center;
    padding: 0 20px;
    color: #999;
    height: 44px;
    .start {
      flex: 1;
      display: flex;
      height: 44px;
      align-items: center;
    }
    .end {
      width: 30%;
      padding-left: 20px;
    }
    .date {
      display: flex;
      flex-direction: column;
      .tip {
        font-size: 12px;
        color: #999;
      }
      .time {
        margin-top: 3px;
        color: #333;
        font-size: 15px;
        font-weight: 500;
      }
    }
  }
  .date-range {
    .stay {
      flex: 1;
      text-align: center;
      font-size: 12px;
      color: #666;
    }
  }
  .price-counter {
    .start {
      border-right: 1px solid var(--line-color);
    }
  }

  .hot-suggests {
    margin: 10px 0;
    height: auto;

    .item {
      padding: 4px 8px;
      margin: 4px;
      border-radius: 14px;
      font-size: 12px;
      line-height: 1;
    }
  }

  .search-btn {
    .btn {
      width: 342px;
      height: 38px;
      max-height: 50px;
      font-weight: 500;
      font-size: 18px;
      line-height: 38px;
      text-align: center;
      border-radius: 20px;
      color: #fff;
      background-image: var(--theme-linear-gradient);
    }
  }
}
</style>
